#include "gpoint2darray.h"
#include <math.h>

GPoint2dArray::GPoint2dArray()
{
}

GPoint2dArray::GPoint2dArray(const GPoint2dArray &other)
{
    mPointVector.resize(other.count());
    for(int i=0; i<other.count(); i++)
    {
        mPointVector[i] = other.mPointVector[i];
    }
}

GPoint2dArray & GPoint2dArray::operator = (const GPoint2dArray &other)
{
    mPointVector.resize(other.count());
    for(int i=0; i<other.count(); i++)
    {
        mPointVector[i] = other.mPointVector[i];
    }
    return *this;
}

GPoint2d GPoint2dArray::at(int index) const
{
    return mPointVector.at(index);
}

bool GPoint2dArray::set(int index, const GPoint2d &pt)
{
    if(index < 0 || index >= count()) return false;
    
    mPointVector[index] = pt;
    return true;
}

GPoint2d & GPoint2dArray::operator [](int index)
{
    return mPointVector[index];
}

const GPoint2d & GPoint2dArray::operator [](int index) const
{
    return mPointVector[index];
}

void GPoint2dArray::addFirst(const GPoint2d &pt)
{
    mPointVector.insert(mPointVector.begin(), pt);
}

void GPoint2dArray::addLast(const GPoint2d &pt)
{
    mPointVector.push_back(pt);   
}

bool GPoint2dArray::insert(int index, const GPoint2d &pt)
{
    if(index < 0 || index > count()) return false;
    mPointVector.insert(mPointVector.begin()+index, pt);
    return true;
}

bool GPoint2dArray::removeFirst()
{
    if(count() <= 0) return false;
    mPointVector.erase(mPointVector.begin());
    return true;
}

bool GPoint2dArray::removeLast()
{
    if(count() <= 0) return false;
    mPointVector.pop_back();
    return true;
}

bool GPoint2dArray::remove(int index)
{
    if(index < 0 || index >= count()) return false;
    
    mPointVector.erase(mPointVector.begin()+index);
    return true;
}

int GPoint2dArray::ptInPoint(double x, double y, double w, double h)
{
    for(int i=0; i<count(); i++)
    {
        if(fabs(mPointVector[i].x() - x) <= w &&
           fabs(mPointVector[i].y() - y) <= h)
        {
            return i;
        }
    }
    return -1;
}
